<template>
  <a-form ref="formRef" :model="formState">
    <div class="flex justify-end mb-2">
      <a-button class="mr-2" @click="onPrintDiagnosis">打印诊断证明书</a-button>
      <a-button @click="onPrintHoliday">打印休假证明书</a-button>
      <div class="absolute top-[-9999px] hidden">
        <a-button ref="printMe" v-print="{ id: 'printMe' }">打印诊断证明书</a-button>
        <a-button ref="printMe2" v-print="{ id: 'printMe2' }">打印休假证明书</a-button>
        <PrintView id="printMe" :print-data="printData" />
        <PrintView2 id="printMe2" :print-data="printData" />
      </div>
    </div>
    <a-form-item name="content" label="建议">
      <!-- <a-textarea v-model:value="formState.content" style="height: calc(100vh - 600px)" placeholder="请输入" /> -->
      <SelectTags
        v-model:value="formState.content"
        show-setting
        show-new
        :input-or-textarea="false"
        :row-val="20"
        multiple
        type="医嘱字典"
        :search-params="{ blcybh: '证明书' }"
      />
    </a-form-item>
    <div class="flex justify-end mt-4">
      <a-button class="mr-4" @click="formState.content = ''">重置</a-button>
      <a-button type="primary" :disabled="!formState.content" @click="onSubmit">保存</a-button>
    </div>
  </a-form>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SelectTags from '@/components/SelectTags'
import useStore from '../../store'
import PrintView from './PrintView'
import PrintView2 from './PrintView2'

const store = useStore()
const formRef = ref(null)
const formState = ref({})
const printData = ref()
const printMe = ref()
const printMe2 = ref()

const getData = async () => {
  const res = await post('/outp/mzys/getOutpatientCertificate', {
    brId: store.currentPatient.brId,
    content: formState.value.content,
    jzbh: store.currentPatient.jzbh
  })
  if (res.code !== 0) return message.error(res.msg)
  formState.value.content = res.data.content
  formState.value.id = res.data.id
}

const onSubmit = async () => {
  const res = await post('/outp/mzys/saveOutpatientCertificate', {
    brId: store.currentPatient.brId,
    content: formState.value.content,
    jzbh: store.currentPatient.jzbh,
    id: formState.value.id || undefined
  })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
const isArmy = getSystemConfig('NAVYHIS_MODE') === '1' //军用系统

const onPrintDiagnosis = async () => {
  const { code, msg, data } = await post('/outp/outPrint/getOutpatientCertificateDocuments', {
    mzjlbh: store.currentPatient.jzbh,
    pjhm: store.currentPatient.pjhm
  })
  if (code !== 0) return message.error(msg)
  if (isArmy) {
    printData.value = JSON.parse(data)
    setTimeout(() => {
      printMe.value.$el.click()
    }, 1000)
  } else {
    window.openExe?.rpt(data) //调用winform打印
  }
}

const onPrintHoliday = async () => {
  const { code, msg, data } = await post('/outp/outPrint/getOutpatientFurloughDocuments', {
    mzjlbh: store.currentPatient.jzbh,
    pjhm: store.currentPatient.pjhm
  })
  if (code !== 0) return message.error(msg)
  if (isArmy) {
    printData.value = JSON.parse(data)
    setTimeout(() => {
      printMe2.value.$el.click()
    }, 1000)
  } else {
    window.openExe?.rpt(data) //调用winform打印
  }
}
onMounted(() => {
  getData()
})
</script>
